//
//  HomeCell.swift
//  Stauts
//
//  Created by Kylin on 16/3/19.
//  Copyright © 2016年 Kylin. All rights reserved.
//

import UIKit
import HYLabel


class HomeCell: UITableViewCell {

    
    // MARK:- 控件属性
    @IBOutlet weak var iconImageView: UIImageView!
    @IBOutlet weak var verifiedImageView: UIImageView!
    @IBOutlet weak var screenNameLabel: UILabel!
    @IBOutlet weak var vipImageView: UIImageView!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var contentLabel: HYLabel!
    @IBOutlet weak var sourceLabel: UILabel!
    @IBOutlet weak var bottomBar: UIView!
    @IBOutlet weak var picCollectionView: PicCollectionView!
    @IBOutlet weak var retweetedContentLabel: HYLabel!
    @IBOutlet weak var retweetedBgView: UIView!
    
    // MARK:- 约束属性
    @IBOutlet weak var contentLabelWidthCons: NSLayoutConstraint!
    @IBOutlet weak var retweetedLabelTopCons: NSLayoutConstraint!
    @IBOutlet weak var picCollectionViewTopCons: NSLayoutConstraint!
    
   
    // MARK:- 定义模型对象的属性
    var statusItemViewModel : HomeStatusItemViewModel? {
        didSet {
            // nil检验
            guard let statusItemViewModel = statusItemViewModel else {
                return
            }
            
             // 2.设置头像
            iconImageView.sd_setImageWithURL(statusItemViewModel.iconURL, placeholderImage: UIImage(named: "avatar_default"))
            // 3:设置认证图标
            verifiedImageView.image = statusItemViewModel.verifiedImage
            // 4:设置昵称
            screenNameLabel.text = statusItemViewModel.statusItem?.userItem?.screen_name
            // 5.设置VIP图标
            vipImageView.image = statusItemViewModel.vipImage
            // 6.设置时间的Label
            timeLabel.text = statusItemViewModel.createAtString
            // 7.设置来源
            sourceLabel.text = statusItemViewModel.sourceText
            // 8.设置微博正文
            contentLabel.attributedText = FindEmoticonString.shareManager.findEmoticonString((statusItemViewModel.statusItem?.text)!, fontHeight: contentLabel.font.lineHeight)
            // 9.设置picCollectionView的picURLs
            picCollectionView.picUrls = statusItemViewModel.picURLs
            // 10.转发的内容
            if statusItemViewModel.statusItem?.retweeted_status != nil {
                
                // 设置转发微博正文
                retweetedContentLabel.text = "@" + (statusItemViewModel.statusItem?.retweeted_status?.userItem?.screen_name)! + ":  " + (statusItemViewModel.statusItem?.retweeted_status?.text ?? "")
                // 2.修改转发微博的Label距离顶部的约束
                retweetedLabelTopCons.constant = 15
                // 3.设置转发背景显示
                retweetedBgView.hidden = false
                
            }else {
                // 1:防止循环利用
                retweetedContentLabel.text = nil
                // 2.修改转发微博的Label距离顶部的约束
                retweetedLabelTopCons.constant = 0
                // 3.设置转发背景显示
                retweetedBgView.hidden = true
            }
           // 11:判断转发微博是否有图片
            picCollectionViewTopCons.constant = statusItemViewModel.picURLs.count == 0 ? 0 : 10
        }
    }
    

    // MARK:- 系统回调函数
    override func awakeFromNib() {
        super.awakeFromNib()
      contentLabelWidthCons.constant = UIScreen.mainScreen().bounds.width - 2 * edageMargin
   
    }

    func cellHeight(statusViewModel : HomeStatusItemViewModel) -> CGFloat {
        // 给模型属性赋值
        self.statusItemViewModel = statusViewModel
        // 2.更新所有的约束
        self.layoutIfNeeded()
        // 3.拿到底部工具栏最大的Y值
        return CGRectGetMaxY(bottomBar.frame)
    }
}

